<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SaveTicket</title>
<!--#include file="ErrorHandler.asp" -->
<% 'on error resume next %>

</head>

<body>

<table width="490" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="20" height="20" background="Images/TableUpperLeft.png">&nbsp;</td>
    <td height="20" background="Images/TableUpperMiddle.png">&nbsp;</td>
    <td width="20" height="20" background="Images/TableUpperRight.png">&nbsp;</td>
  </tr>
  <tr>
    <td width="20" background="Images/TableMiddleLeft.png">&nbsp;</td>
    <td align="center" valign="middle">Saving Ticket...Please Wait </td>
    <td width="20" background="Images/TableMiddleRight.png">&nbsp;</td>
  </tr>
  <tr>
    <td width="20" height="20" background="Images/TableLowerLeft.png">&nbsp;</td>
    <td height="20" background="Images/TableLowerMiddle.png">&nbsp;</td>
    <td width="20" height="20" background="Images/TableLowerRight.png">&nbsp;</td>
  </tr>
</table>

<%
if session("ticketsaved")<>0 then response.Redirect(session("redir"))

TicketID=trim(request.Form("Ticket_ID"))
ContractID=trim(request.Form("Contract_ID"))
Appliance=trim(request.Form("Appliance"))
Stat=trim(request.Form("Status"))
Emergency=trim(request.Form("Emergency"))
Notes=trim(request.Form("Notes"))
Notes=replace(Notes,"'","`")
Notes=replace(Notes,chr(34),"`")
user=trim(request.QueryString("user"))
FromDispatch=trim(request.QueryString("FromDispatch"))
session("ticketsaved")=1

dim db,rs
set db=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")

FilePath=server.MapPath("\afterhours\access\backend\tickets.mdb")
db.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath

'------------------------------NEW TICKET -----------------------------------
if TicketID="" then
	rs.open "select max(ticket_id) as maxticketid from tickets",db,1,2
	NextTicketID=rs("maxticketid")+1
	if not isnumeric(NextTicketID) then NextTicketID=1
	rs.close

	rs.open "select top 1 * from tickets",db,1,2
		rs.addnew
			rs("ticket_id")=NextTicketID
			rs("Contract_ID")=ContractID
			rs("Status")=Stat
			rs("Created_Time")=Now()
			rs("Appliance")=Appliance
			rs("Emergency")=Emergency
		rs.update
	rs.close
	
	rs.open "select max(note_id) as maxnoteid from ticket_history",db,1,2
	NextNoteID=rs("maxnoteid")+1
	if not isnumeric(NextNoteID) then NextNoteID=1
	rs.close

	rs.open "select top 1 * from ticket_history",db,1,2
		rs.addnew
			rs("Note_ID")=NextNoteID
			rs("Ticket_ID")=NextTicketID
			rs("User")=user
			rs("When")=now()
			rs("Note")=Notes
		rs.update
	rs.close
	
	session("redir")="TicketWindow.asp?Ticket_ID=" & NextTicketID & "&user=" & user
	
	'----------send email

	set senddispatchemailrs=server.CreateObject("adodb.recordset")
	set senddispatchemailconn=server.CreateObject("adodb.connection")

	EmailFilePath=server.MapPath("access\backend\admin.mdb")
	senddispatchemailconn.open ("provider=microsoft.jet.oledb.4.0;data source=" & EmailFilePath)
	
	senddispatchemailrs.cursorlocation=3 'adUseClient
	senddispatchemailrs.cursortype=0 'adOpenForwardOnly 
	senddispatchemailrs.locktype=1 'adLockReadOnly 
	
	Sendsql="select top 1 * from DispatchEmail" 
	senddispatchemailrs.open Sendsql,senddispatchemailconn 

	send_dispatch_email=senddispatchemailrs("send_dispatch_email_new")
	dispatch_email=senddispatchemailrs("dispatch_email")
	senddispatchemailrs.close
	senddispatchemailconn.close
	set senddispatchemailrs=nothing
	set senddispatchemailconn=nothing
	
		'----------- get email server --------------
		if application("EmailServer")="" or isnull(application("EmailServer")) then server.Execute("GetEmailServer.asp")	
		'--------- end get email server -------------
		
		noserver=0
		if isnull(application("EmailServer")) or application("EmailServer")="" then noserver=1
		if (send_dispatch_email=true) and (noserver=0) then
		'send mail configuration
		sch = "http://schemas.microsoft.com/cdo/configuration/" 
    
    		Set cdoConfig = CreateObject("CDO.Configuration") 
    
    		With cdoConfig.Fields 
			.Item(sch & "sendusing") = 2 ' cdoSendUsingPort 
    			.Item(sch & "smtpserver") = application("EmailServer")  
    			.update 
    		End With 
	
	 	Set cdoMessage = CreateObject("CDO.Message") 

		set email=server.CreateObject("cdo.message")
		with email
			Set .Configuration = cdoConfig
			.subject="HomeSmart afterhours: New ticket"
			.from="Ticket_Auto_Sender"
			
			'-----make the email body
			url="http://" & request.ServerVariables("HTTP_HOST") & "/afterhours/accountticketframeset.asp" & "?FromDispatch=yes&ticketid=" & NextTicketID &"&ContractID=" & contractid & "&user=" & user
			b=" A new ticket has been created.<br><br>"
			b=b & "<a href='" & url & "'>Click Here To Open Ticket</a><br><br>"
			b=b & "Ticket Number:	" & nextticketid & "<br>"
			b=b & "Status:		" & stat & "<br>"
			b=b & "Created Time:	" & now() & "<br>"
			b=b & "Appliance:		" & appliance & "<br>"
			b=b & "Is Emergency:	" & emergency & "<br>"
			b=b & "User:	" & user & "<br>"
			b=b & "Notes:	" & notes
				
			.htmlbody=b
			.to=dispatch_email
			set cdoconfig=nothing
		
			.Send
			if err.number<>0 then ErrorHandler
			set email=nothing
			set cdoConfig=nothing
		end with
	end if


'----------------------- NOT NEW TICKET SO SAVE EXISTING TICKET --------------------
else
	rs.open "select * from tickets where ticket_id=" & TicketID,db,1,2
	if rs.eof then
		%><script type="text/javascript">alert("There Was An Error Identifying this Ticket Number\nPlease Re-Open This Ticket, Make Your Entry, And Try Saving Again.")</script>
		<%
		session("redir")="TicketWindow.asp?Ticket_ID=" & TicketID & "&user=" & user
	else
		rs("Status").value=Stat
		rs("Appliance").value=Appliance
		rs("Emergency").value=Emergency
		
		'IS THE WINDOW OPENED FROM DISPATCH
		if request.QueryString("FromDispatch")="yes" then
			rs("Given_To")=request.form("GivenTo")
			if request.form("GivenToTime")<>"" then
				rs("Dispatched_Time")=cdate(request.Form("GivenToTime"))
			else
				rs("Dispatched_Time")=null
			end if
			
			if isnull(request.Form("followup")) then
				followupval=""
			else
				followupval=request.Form("followup")
			end if
			rs("follow_up")=followupval
		end if
		rs.update
		rs.close
		rs.open "select max(note_id) as maxnoteid from ticket_history",db,1,2
		NextNoteID=rs("maxnoteid")+1
		rs.close

		rs.open "select top 1 * from ticket_history",db,1,2
		rs.addnew
			rs("Note_ID")=NextNoteID
			rs("Ticket_ID")=TicketID
			rs("User")=user
			rs("When")=now()
			rs("Note")=Notes
		rs.update
		
		rs.close
		set rs=nothing
		
		session("redir")="TicketWindow.asp?Ticket_ID=" & TicketID & "&user=" & user
	
'----------- send email
		
		set senddispatchemailrs=server.CreateObject("adodb.recordset")
		set senddispatchemailconn=server.CreateObject("adodb.connection")

		EmailFilePath=server.MapPath("access\backend\admin.mdb")
		senddispatchemailconn.open ("provider=microsoft.jet.oledb.4.0;data source=" & EmailFilePath)
	
		senddispatchemailrs.cursorlocation=3 'adUseClient
		senddispatchemailrs.cursortype=0 'adOpenForwardOnly 
		senddispatchemailrs.locktype=1 'adLockReadOnly 
	
		Sendsql="select top 1 * from DispatchEmail" 
		senddispatchemailrs.open Sendsql,senddispatchemailconn

			send_dispatch_email=senddispatchemailrs("send_dispatch_email_existing")
			dispatch_email=senddispatchemailrs("dispatch_email")
		
		senddispatchemailrs.close
		senddispatchemailconn.close
		set senddispatchemailrs=nothing
		set senddispatchemailconn=nothing
		
		'----------- get email server --------------
		if application("EmailServer")="" or isnull(application("EmailServer")) then server.Execute("GetEmailServer.asp")	
		'--------- end get email server -------------
		
		noserver=0
		if isnull(application("EmailServer")) or application("EmailServer")="" then noserver=1
		if (send_dispatch_email=true) and (noserver=0) then
			'send mail configuration
			sch = "http://schemas.microsoft.com/cdo/configuration/" 
    
    			Set cdoConfig = CreateObject("CDO.Configuration") 
    
    			With cdoConfig.Fields 
				.Item(sch & "sendusing") = 2 ' cdoSendUsingPort 
    				.Item(sch & "smtpserver") = application("EmailServer") 
    				.update 
    			End With 


			set email=server.CreateObject("cdo.message")
			with email
				Set .Configuration = cdoConfig
				.subject="HomeSmart afterhours: Update to ticket#" & ticketid
				.from="Ticket_Auto_Sender"
				
				'-----make the email body
				url="http://" & request.ServerVariables("HTTP_HOST") & "/afterhours/accountticketframeset.asp" & "?FromDispatch=yes&ticketid=" & ticketid &"&ContractID=" &contractid & "&user=" & user
				b="Ticket# " & ticketid & " has been modified.<br><br>"
				b=b & "<a href='" & url & "'>Click Here To Open Ticket</a><br><br>"
				b=b & "Ticket Number:	" & ticketid & "<br>"
				b=b & "Status:		" & stat & "<br>"
				b=b & "Is Emergency:	" & emergency & "<br>"
				b=b & "User:	" & user & "<br>"
				b=b & "Notes:	" & notes
				
				.htmlbody=b
				.to=dispatch_email

				.Send

					
				if err.number<>0 then 
					ErrorHandler
				else			
				end if

				set email=nothing
				set cdoconfig=nothing
			end with
		end if 'if send email=true
		
	end if 'if ticket ID was found
	
end if 'if request.form("Ticket_ID") was blank or not

db.close
set db=nothing
%>

<script type="text/javascript">
<% 
if FromDispatch<>"yes" then 
	%>
	parent.parent.HistoryFrame.location="TicketHistory.asp?Contract_ID=<%= ContractID %>"
	<% 
else
	session("redir")=session("redir") & "&FromDispatch=yes"
end if 
%>

window.location.href="<%= session("redir") %> "

</script>


</body>
</html>
